package com.culturalCenter.dataCenter.service.impl.user;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.culturalCenter.dataCenter.Utils.DateFormatUtil;
import com.culturalCenter.dataCenter.Utils.MapperUtil;
import com.culturalCenter.dataCenter.customEntity.responseParam.user.UserInfoResponse;
import com.culturalCenter.dataCenter.customEntity.responseParam.user.UserListResponse;
import com.culturalCenter.dataCenter.customEntity.responseParam.user.UserMenuResponse;
import com.culturalCenter.dataCenter.customEntity.user.CustomListUser;
import com.culturalCenter.dataCenter.entity.user.*;
import com.culturalCenter.dataCenter.mapper.activity.ActivityMapper;
import com.culturalCenter.dataCenter.mapper.site.SiteMapper;
import com.culturalCenter.dataCenter.mapper.training.TrainingMapper;
import com.culturalCenter.dataCenter.mapper.user.*;
import com.culturalCenter.dataCenter.service.user.UserFootprintService;
import com.culturalCenter.dataCenter.service.user.UserLocationService;
import com.culturalCenter.dataCenter.service.user.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.thoughtworks.paranamer.ParameterNamesNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User业务层接口实现类
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private UserDayNumberMapper userDayNumberMapper;
    @Resource
    private UserTypeCountMapper userTypeCountMapper;
    @Resource
    private UserAgeCountMapper userAgeCountMapper;
    @Resource
    private UserZodiacForeignMapper userZodiacForeignMapper;
    @Resource
    private UserZodiacChinaMapper userZodiacChinaMapper;
    @Resource
    private UserInfoCountMapper userInfoCountMapper;
    @Resource
    private UserFootprintMapper userFootprintMapper;
    @Resource
    private UserActivityMapper userActivityMapper;
    @Resource
    private UserLocationService userLocationService;
    @Resource
    private UserFootprintService userFootprintService;
    @Resource
    private MapperUtil mapperUtil;
    @Resource
    private SiteMapper siteMapper;
    @Resource
    private TrainingMapper trainingMapper;
    @Resource
    private ActivityMapper activityMapper;

    @Override
    public PageInfo<User> findPage(int page, int size) {
        return PageHelper.startPage(page, size).doSelectPageInfo(() -> userMapper.findPageAll());
    }

    @Override
    public Integer delete(String id) {
        return userMapper.deleteById(id);
    }

    @Override
    public Integer update(User user) {
        return userMapper.update(user);
    }

    @Override
    public Integer add(User user) {
        return userMapper.insert(user);
    }

    @Override
    public User findById(String id) {
        return userMapper.queryById(id);
    }

    @Override
    public List<User> findAll(User user) {
        return (List<User>) userMapper.queryAll(user);
    }

    @Override
    public UserMenuResponse getMenuData() {
        UserMenuResponse userMenuResponse = new UserMenuResponse();
        UserDayNumber userDayNumber = userDayNumberMapper.selectByDate(DateFormatUtil.format(new Date()));
        userMenuResponse.setTotal(userMapper.getTotal());
        userMenuResponse.setMonthTotal(userMapper.getThisMonthUsers());
        if (ObjectUtil.isNotEmpty(userDayNumber)) {
            userMenuResponse.setTodayNum(userDayNumber.getDayIncreaseNum());
        }

        List<UserDayNumber> userDayNumbers = userDayNumberMapper.selectByMonth(DateFormatUtil.formatMonth(new Date()));
        userMenuResponse.setDayTotalList(userDayNumbers);

        Object oneResult = mapperUtil.getOneResult(userTypeCountMapper);
        if (ObjectUtil.isNotEmpty(oneResult)) {
            userMenuResponse.setTypeCount((UserTypeCount) oneResult);
        }

        List<UserAgeCount> userAgeCounts = userAgeCountMapper.findAll();
        if (CollUtil.isNotEmpty(userAgeCounts)) {
            userMenuResponse.setAgeScopeList(userAgeCounts);
        }

        userMenuResponse.setZodiacForeign((UserZodiacForeign) mapperUtil.getOneResult(userZodiacForeignMapper));
        userMenuResponse.setZodiacChina((UserZodiacChina) mapperUtil.getOneResult(userZodiacChinaMapper));
        userMenuResponse.setCustomListUser(userMapper.selectActiveUserList());
        return userMenuResponse;
    }

    @Override
    public UserListResponse findUserList(String branchId,String username, int pageNum, int pageSize) {
        UserListResponse userListResponse = new UserListResponse();
        PageHelper.startPage(pageNum, pageSize);
        //        BeanUtils.copyProperties();
        PageInfo<CustomListUser> pageInfo = new PageInfo<>(userMapper.findCustomListUser(username,branchId));
        PageHelper.clearPage();
        userListResponse.setPageInfo(pageInfo);
        userListResponse.setTotal((int) pageInfo.getTotal());
        return userListResponse;
    }

    @Override
    public UserInfoResponse findUserInfo(String userId) {
        UserInfoResponse userInfoResponse = new UserInfoResponse();
        User user = userMapper.getByUserId(userId);
        if (user != null) {
            userInfoResponse.setId(user.getId());
            userInfoResponse.setName(user.getName());
            userInfoResponse.setPhoto(user.getPhoto());
            userInfoResponse.setIDCard(user.getIDCard());
            userInfoResponse.setPhone(user.getPhone());
            userInfoResponse.setLicencePlate(user.getLicencePlate());
            userInfoResponse.setSex(user.getSex());
            userInfoResponse.setType(user.getType());
            userInfoResponse.setZodiacForeign(user.getZodiacForeign());
            userInfoResponse.setZodiacChina(user.getZodiacChina());
            userInfoResponse.setAge(user.getAge());
            userInfoResponse.setPoint(user.getPoint());
            userInfoResponse.setLevel(user.getLevel());
            userInfoResponse.setBlackList(user.getIsBlackList());
        }

        UserInfoCount userInfoCount = (UserInfoCount) mapperUtil.getOneResult(userInfoCountMapper, userId);
        if (userInfoCount != null) {
            userInfoResponse.setActivityNum(userInfoCount.getActivityNum());
            userInfoResponse.setTrainNum(userInfoCount.getTrainNum());
            userInfoResponse.setVisitWebTime(userInfoCount.getVisitWebTime());
            userInfoResponse.setVisitSiteTime(userInfoCount.getVisitSiteTime());
            userInfoResponse.setJoinActivityTime(userInfoCount.getJoinActivityTime());
        }

        userInfoResponse.setRecentFootprintList(userFootprintMapper.findCustomUserRecent(userId));
        userInfoResponse.setRecentActivityList(userActivityMapper.findCustomUserRecent(userId));
        // 和现场记录是同个接口
        userInfoResponse.setRecentLocationList(userLocationService.findLocationList(userId, 1, 3).getList());
        return userInfoResponse;
    }

    @Override
    public Map<String, List<CustomListUser>> getUserAnalysis(String type) {
        Map<String, List<CustomListUser>> userAnalyses = new HashMap<>();

        switch (type) {
            case "活动":
                List<String> activityTypes = activityMapper.getActivityTypeList(null, null, null);
                activityTypes.removeIf(StrUtil::isBlank);
                activityTypes.forEach(activityType -> userAnalyses.put(activityType, activityMapper.getUsersByActivityType(activityType)));
                return userAnalyses;
            case "培训":
                List<String> trainingTypes = trainingMapper.getTypes();
                trainingTypes.removeIf(StrUtil::isBlank);
                trainingTypes.forEach(trainingType -> userAnalyses.put(trainingType, trainingMapper.getUsersByTrainingType(trainingType)));
                return userAnalyses;
            case "场地":
                List<String> siteTypes = siteMapper.getSiteTypeList();
                siteTypes.removeIf(StrUtil::isBlank);
                siteTypes.forEach(siteType -> userAnalyses.put(siteType, siteMapper.getUsersBySiteType(siteType)));
                return userAnalyses;
            default:
                throw new ParameterNamesNotFoundException("没有此聚合类型");
        }
    }

}
